package com.lkm.app_ui.view.choiceCard;


import java.util.HashSet;
import java.util.Set;

public class ChoiceItem implements IChoiceItem {

	private Object id;
	private String name;
	private String value;
	private boolean isChoice;

	public ChoiceItem() {
	}

	public ChoiceItem(String id, String name, boolean isChoice) {
		this(id, name);
		setIsChoice(isChoice);
	}
	public ChoiceItem(String id, String name, String value) {
		this(id, name);
		setValue(value);
	}

	public ChoiceItem(String id, String name) {

		setID(id);
		setName(name);
		setValue(""+id);
	}
	
	@Override
	public Object getID() {
		return id;
	}

	@Override
	public void setID(Object id) {
		this.id = id;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public IChoiceItem setValue(String v) {
		this.value = v;
		return this;
	}

	@Override
	public String getValue() {
	return value == null ? (getID()==null?"": getID().toString()) : value;
}

	@Override
	public void setIsChoice(boolean isChoice) {
		this.isChoice = isChoice;
	}

	@Override
	public boolean getIsChoice() {
		return isChoice;
	}


	private Object tag;
	@Override
	public Object getTag() {
		return tag;
	}

	@Override
	public void setTag(Object o) {
		this.tag=o;
	}

	public static String getChoiceForName(IChoiceItem[] cs, boolean isMu, String spit) {
		return getChoice(cs, isMu, false,spit);
	}

	

	public static String getChoice(IChoiceItem[] cs, boolean isMu, boolean isId,String spit) {
		String s = null;
		if (cs == null)
			return null;
		for (IChoiceItem c : cs) {
			if (c.getIsChoice()) {
				if (!isMu)
					return isId ? ("" + c.getID()) : c.getValue();
				else {
					if (s == null)
						s = isId ? ("" + c.getID()) : c.getValue();
					else
						s += spit + (isId ? ("" + c.getID()) : c.getValue());
				}
			}
		}

		return s;
	}

	

	public static void setChoiceState(IChoiceItem[] cs, String[] choices, boolean isUseName) {
		if (cs!=null) {
			if (choices!=null) {
				Set<String> sset = new HashSet<String>();
				for (String s : choices)
					sset.add(s);
				for (IChoiceItem ci : cs) {
					if (ci != null)
						ci.setIsChoice(  sset.contains(isUseName ? ci.getName() : ci.getValue()));
				}
			}
		}
	}

}
